/**	\author    coidgen 0.9.2
	\version   0.1 (8.0 build 1)
	\date      
	\brief     
*/



// Service header info (for version checking),  ! DO NOT CHANGE !
static const char COID_bin_data[] =
"060005000000302E392E3209000000312E342028342E3029C70E0000FE325B4A302E3100000000000008000001000000"
"09000000010000000700000057656274616C6B0000000013000000206E6F6C6F675F73797374656D5F63616C6C730000"
"0000040000000400000004000000FFFFFFFF040000006F70636400000000000000000000800000000004000000726561"
"640000000003000000FFFFFFFF0400000075696E74000000000000000000000408000000FFFFFFFF0F00000064796E61"
"727261793C746F6B656E3E000000000000000000000208000000FFFFFFFF0400000075696E7400000000000000000100"
"0204000000000207200000000000000000FFFFFFFF040000006F70636400000000000000000000800000000004000000"
"73656E640100000001000000FFFFFFFF0700000063686172737472000000000000000000000108000000000207200000"
"000000000000FFFFFFFF040000006F70636400000000000000000000800000000008000000757365726C697374020000"
"0001000000FFFFFFFF0700000063686172737472000000000000000000000208000000000207200000000000000000FF"
"FFFFFF040000006F70636400000000000000000000800000000006000000736D696C65730300000001000000FFFFFFFF"
"16000000636F6E73742064796E61727261793C536D696C65793E00000000000000000100180C00000000020720020000"
"000000000001FFFFFFFF040000006F7063640000000000000000000080000000000E0000006163636570745F636F6E6E"
"6563740500008004000000FFFFFFFF0D000000636F6E73742063686172737472000000000000000000001108000000FF"
"FFFFFF0D000000636F6E73742063686172737472000000000000000000001108000000FFFFFFFF0E000000636F6E7374"
"2070617373776F7264000000000000000000001108000000FFFFFFFF0400000075696E74000000000000000001000204"
"00000000028720000000000000000001FFFFFFFF040000006F7063640000000000000000000080000000001500000061"
"63636570745F636F6E6E6563745F7368617265640600008000000000000087000000000000000000000001FFFFFFFF04"
"0000006F7063640000000000000000000080000000000E0000006163636570745F64657374726F790200008000000000"
"00028700000000000000000001FFFFFFFF040000006F7063640000000000000000000080000000001500000061636365"
"70745F737461727475705F706172616D73FFFF008001000000FFFFFFFF0D000000636D645F696E746572666163650000"
"0000000000000000010800000000008720000000000000000000000001002700FFFFFFFF00000000"
;
//*/


#include "webtalk.h"
#include "coid/comm/metastream/metastream.h"


namespace coid {


static inline const char ** COID_OUT_ARGS(const char ** from) {
	while( *from ) from++;
	return ++from;
}

typedef ServiceDescriptor::MethodDesc MethodDesc;
#define COID_MethodDesc_constructor(S,I,F)         MethodDesc( S[0], S[1], &S[2], COID_OUT_ARGS(&S[2]), I, F )
#define COID_MethodDesc_constructor_NotImpl        MethodDesc( NULL, NULL, NULL, NULL, 0, 0 )



typedef void (Webtalk::*coid_fnc)();

/// fake function, should never be called:
static void coid_throw() {throw ersFE_EXCEPTION;}

/// we need type cast in case there are 2 or more methods with the same name
#define COID_DISPATCH_CREATE_HOOK(M,i,t,tc) \
	if( i < coid_fnc_count ) { \
		if( (coid_flags[i] & COID_Webtalk_method_desc[i].flags & MethodDesc::xACCESS_MODE) ) \
			coid_fnc_table[t] = (coid_fnc) (tc) &Webtalk::M; \
		else *(void **) &coid_fnc_table[t] = (void *) coid_throw; \
	}


#define COID_DISPATCH_CREATE_CALLER_HOOK(M,i,t) \
	if( i < coid_fnc_count ) { \
		if( (coid_flags[i] & COID_Webtalk_method_desc[i].flags & MethodDesc::xACCESS_MODE) ) \
			coid_fnc_table[t] = (coid_fnc) &DISPATCH_CALLER_Webtalk_8_0::M; \
		else *(void **) &coid_fnc_table[t] = (void *) coid_throw; \
	}






/// functions:
uint COID_Webtalk_8_0_get_method_id( const token & name );
opcd COID_Webtalk_8_0_dispatch( InstanceData * _coid_inst_data, uint _coid_fnc_id, binstream& _coid_bstr, metastream* coid_meta=0 );
metastream& COID_Webtalk_8_0_getmeta();



struct DISPATCH_CALLER_Webtalk_8_0
{
	void call_dispatch( binstream * b ) {
		COID_CLIENT_EXESTREAM_ARGS * a = (COID_CLIENT_EXESTREAM_ARGS *) this;
		InstanceData d;  d._object = a->obj;
		COID_Webtalk_8_0_dispatch( &d, a->id, *b );
	}
};





enum {	/// command ids, format is: COID_classname_fncname_fncid
	COID_Webtalk_read_0 = 0,
	COID_Webtalk_send_1 = 1,
	COID_Webtalk_userlist_2 = 2,
	COID_Webtalk_smiles_3 = 3,

    COID_Webtalk_accept_connect_5,
    COID_Webtalk_accept_connect_shared_6
};



/// info about every function:
static const char * COID_Webtalk_read_0_finfo[] = {"read", "opcd ", "uint & ref", NULL, "uint & ref", "dynarray<token> & rows", "uint * flg", NULL};
static const char * COID_Webtalk_send_1_finfo[] = {"send", "opcd ", "charstr & text", NULL, NULL};
static const char * COID_Webtalk_userlist_2_finfo[] = {"userlist", "opcd ", NULL, "charstr & usrs", NULL};
static const char * COID_Webtalk_smiles_3_finfo[] = {"smiles", "opcd ", NULL, "dynarray<Smiley> & smi", NULL};
static const char * COID_Webtalk_accept_connect_5_finfo[] = {"accept_connect", "opcd ", "const charstr & path", "const charstr & name", "const password & pwd", NULL, "uint * pos", NULL};
static const char * COID_Webtalk_accept_connect_shared_6_finfo[] = {"accept_connect_shared", "opcd ", NULL, NULL};


/// create structure that holds everything about function:
static const MethodDesc COID_Webtalk_method_desc[] = {
	COID_MethodDesc_constructor( COID_Webtalk_read_0_finfo, 0x00000000, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fMETA | MethodDesc::fLOG_CALL ),
	COID_MethodDesc_constructor( COID_Webtalk_send_1_finfo, 0x00000001, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fMETA | MethodDesc::fLOG_CALL ),
	COID_MethodDesc_constructor( COID_Webtalk_userlist_2_finfo, 0x00000002, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fMETA | MethodDesc::fLOG_CALL ),
	COID_MethodDesc_constructor( COID_Webtalk_smiles_3_finfo, 0x00000003, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fMETA | MethodDesc::fLOG_CALL ),
	COID_MethodDesc_constructor( COID_Webtalk_accept_connect_5_finfo, 0x80000005, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fMETA | MethodDesc::fLOG_CALL ),
	COID_MethodDesc_constructor( COID_Webtalk_accept_connect_shared_6_finfo, 0x80000006, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fLOG_CALL )
};



static const char * COID_dependencies_Webtalk[] = {

	NULL
};


/// ServiceDescriptor:
static ServiceDescriptor COID_descriptor_Webtalk;



/// dispatch stores function tables for all 'direct' clients with different minor versions here
class COID_register_Webtalk_8_0
{
	struct table {
		uint ver;
		coid_fnc * tbl;
	};
	dynarray<table>	_tables;

public:
//	comm_mutex_rw	_mx;
	comm_mutex	_mx;
   const void* (*dummyreg)();

	COID_register_Webtalk_8_0() {
		COID_descriptor_Webtalk._version.set( "0.1", 8, 0, 1 );
		COID_descriptor_Webtalk._coid_version = COID_VERSION;
		COID_descriptor_Webtalk._name = "Webtalk";
		COID_descriptor_Webtalk._shortname = "Webtalk";
		COID_descriptor_Webtalk._info = " ";
		COID_descriptor_Webtalk._dependencies = COID_dependencies_Webtalk;
		COID_descriptor_Webtalk._nmeth = 4;
		COID_descriptor_Webtalk._methflags = COID_Webtalk_method_desc;
		COID_descriptor_Webtalk._flags = ServiceDescriptor::fALLOW_DIRECT_ACCESS | ServiceDescriptor::fALLOW_INTERPROCESS_ACCESS | ServiceDescriptor::fALLOW_REMOTE_ACCESS | ServiceDescriptor::fAUTONOMOUS;
		COID_descriptor_Webtalk._acceptor_port = (uint) -1;
		COID_descriptor_Webtalk._bin_svc_data = COID_bin_data;
		COID_descriptor_Webtalk.get_method_id = COID_Webtalk_8_0_get_method_id;
		COID_descriptor_Webtalk.dispatch = COID_Webtalk_8_0_dispatch;
		COID_descriptor_Webtalk._fnc_get_meta = COID_Webtalk_8_0_getmeta;
#ifdef _DEBUG
		COID_descriptor_Webtalk._flags |= ServiceDescriptor::fDEBUG;
#endif
		_mx.set_name( "COID_register_Webtalk_8_0" );
		INTERFACE_REGISTER(ServiceDescriptor).add( &COID_descriptor_Webtalk );
	}

	~COID_register_Webtalk_8_0() {
//		MXGUARDRW( (comm_mutex_rw &) _mx );
		MXGUARD( (comm_mutex &) _mx );
		for( uint i=0; i<_tables.size(); i++ )
			delete [] _tables[i].tbl;
	}
	coid_fnc * get_table_notlocked( uint ver ) {
		for( uint i=0; i<_tables.size(); i++ )
			if( ver == _tables[i].ver ) return _tables[i].tbl;
		return NULL;
	}
	coid_fnc * get_table( uint ver ) {
//		MXGUARDRW( (const comm_mutex_rw &) _mx );		/// ==> read lock
		MXGUARD( (const comm_mutex &) _mx );
		return get_table_notlocked( ver );
	}
	void insert_table( uint ver, coid_fnc * tbl ) {
		table * t = _tables.add( 1 );	/// mutex has been locked
		t->ver = ver; t->tbl = tbl;
	}
};


// for dll linking
COID_register_Webtalk_8_0 & get_COID_register_Webtalk_8_0()
{
	static COID_register_Webtalk_8_0 x;
	x.dummyreg = &COID_GET_FN;    //force exporting the symbol
	x.dummyreg();
	return x;
}

//for static linking (must be called from outside to link this object)
void get_COID_register_Webtalk()
{
	COID_register_Webtalk_8_0();
}

static COID_register_Webtalk_8_0 & COID_auto_register_variable = get_COID_register_Webtalk_8_0();





metastream& COID_Webtalk_8_0_getmeta()
{
    static metastream* meta = 0;
    if(meta)  return *meta;

    meta = new metastream;
#define META_TD(TY)     metastream::TypeDesc< TY >::get_str(*meta)

    {   //COID_Webtalk_accept_connect_5
        ServiceDescriptor::MethodDesc* med = (ServiceDescriptor::MethodDesc*)&COID_descriptor_Webtalk._methflags[COID_Webtalk_accept_connect_5];
        med->retval.meta = META_TD(opcd);

		med->inargs[0].meta = META_TD(charstr);
		med->inargs[1].meta = META_TD(charstr);
		med->inargs[2].meta = META_TD(password);
		med->outargs[0].meta = META_TD(uint);
    }
    {   //COID_Webtalk_read_0
        ServiceDescriptor::MethodDesc* med = (ServiceDescriptor::MethodDesc*)&COID_descriptor_Webtalk._methflags[COID_Webtalk_read_0];
        med->retval.meta = META_TD(opcd);

		med->inargs[0].meta = META_TD(uint);
		med->outargs[0].meta = META_TD(uint);
		med->outargs[1].meta = META_TD(dynarray<token>);
		med->outargs[2].meta = META_TD(uint);
    }
    {   //COID_Webtalk_send_1
        ServiceDescriptor::MethodDesc* med = (ServiceDescriptor::MethodDesc*)&COID_descriptor_Webtalk._methflags[COID_Webtalk_send_1];
        med->retval.meta = META_TD(opcd);

		med->inargs[0].meta = META_TD(charstr);
    }
    {   //COID_Webtalk_userlist_2
        ServiceDescriptor::MethodDesc* med = (ServiceDescriptor::MethodDesc*)&COID_descriptor_Webtalk._methflags[COID_Webtalk_userlist_2];
        med->retval.meta = META_TD(opcd);

		med->outargs[0].meta = META_TD(charstr);
    }
    {   //COID_Webtalk_smiles_3
        ServiceDescriptor::MethodDesc* med = (ServiceDescriptor::MethodDesc*)&COID_descriptor_Webtalk._methflags[COID_Webtalk_smiles_3];
        med->retval.meta = META_TD(opcd);

		med->outargs[0].meta = META_TD(const dynarray<Smiley>);
    }

    return *meta;
}



///////////////////////////////////////////////////////////////////////////////
opcd COID_Webtalk_8_0_dispatch( InstanceData* _coid_inst_data, uint _coid_fnc_id, binstream& _coid_bstr, metastream* coid_meta )
{
	Webtalk * _coid_Webtalk_obj = (Webtalk *) _coid_inst_data->_object;
	double _coid_Webtalk_ticks = 0.0;
	binstream * _coid_log = NULL;
	opcd _coid_err_code = 0;

	switch( _coid_fnc_id ) {
		case StdDispatchMethod::CONSTRUCTOR: {
			_coid_inst_data->_object = _coid_Webtalk_obj = new Webtalk;
       } break;
		case StdDispatchMethod::POSTCONSTRUCTOR: {
			_coid_err_code = _coid_Webtalk_obj->accept_startup_params( *_coid_inst_data->_cmdin );
		} break;
		case StdDispatchMethod::DESTRUCTOR: {
			delete _coid_Webtalk_obj; _coid_inst_data->_object = NULL;
		} break;
		case StdDispatchMethod::CREATE_INTERFACE: {
			CreateInterfaceInfo* coid_iface_info;
			_coid_bstr.read_ptr(coid_iface_info);
			const uint coid_fnc_count = coid_iface_info->_nmethods;
			const uint * coid_flags = coid_iface_info->_methflags;
			coid_fnc * coid_fnc_table = get_COID_register_Webtalk_8_0().get_table( coid_iface_info->_clientver.get_minor() );

			if( !coid_fnc_table ) {
				//MXGUARDRW( get_COID_register_Webtalk_8_0()._mx );
				MXGUARD( get_COID_register_Webtalk_8_0()._mx );
				coid_fnc_table = get_COID_register_Webtalk_8_0().get_table_notlocked( coid_iface_info->_clientver.get_minor() );
				if( !coid_fnc_table ) {
					coid_fnc_table = (coid_fnc *) new char[ sizeof(coid_fnc) * (coid_fnc_count + 1) ];	/// new coid_fnc[...] would allocate pointer to 0 bytes (M$ 'feature')
					memset( coid_fnc_table, 0, (coid_fnc_count + 1) * sizeof(coid_fnc) );
					uint coid_table_index = 0;

					((CreateInterfaceInfo::t_fnc *) coid_fnc_table)[coid_table_index++] = coid_iface_info->_disconnect_interface;
					COID_DISPATCH_CREATE_HOOK( read, 0, coid_table_index++, opcd (Webtalk::*)( uint & , dynarray<token> & , uint *  ) )
					COID_DISPATCH_CREATE_HOOK( send, 1, coid_table_index++, opcd (Webtalk::*)( charstr &  ) )
					COID_DISPATCH_CREATE_HOOK( userlist, 2, coid_table_index++, opcd (Webtalk::*)( charstr &  ) )
					COID_DISPATCH_CREATE_HOOK( smiles, 3, coid_table_index++, opcd (Webtalk::*)( dynarray<Smiley> &  ) )

					get_COID_register_Webtalk_8_0().insert_table( coid_iface_info->_clientver.get_minor(), coid_fnc_table );
				}
			}

			_coid_bstr.acknowledge();
			_coid_bstr.write_ptr( coid_fnc_table );
			_coid_bstr.write_ptr( _coid_inst_data->_object );
			_coid_bstr.write_ptr( coid_iface_info->_mutex );
		} break;
		case StdDispatchMethod::AUTHENTIFY: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_Webtalk_ticks = SINGLETON(HPTIMER).ftime();
			
			charstr path;
			if(coid_meta)  coid_meta->xstream_in(path, "path"); else  _coid_bstr >> path;
			charstr name;
			if(coid_meta)  coid_meta->xstream_in(name, "name"); else  _coid_bstr >> name;
			password pwd;
			if(coid_meta)  coid_meta->xstream_in(pwd, "pwd"); else  _coid_bstr >> pwd;
			uint pos;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << "80000005" << ") accept_connect ( "; *_coid_log << path; *_coid_log << ", "; *_coid_log << name; *_coid_log << ", "; *_coid_log << pwd; *_coid_log << ", "; *_coid_log << "?"; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_Webtalk_obj->accept_connect( path, name, pwd, &pos );
			if(coid_meta)  coid_meta->xstream_out(_coid_err_code,"err");  else  _coid_bstr << _coid_err_code;
			if( ! _coid_err_code ) {
				if(coid_meta)  coid_meta->xstream_out( pos, "pos" );  else  _coid_bstr << pos;
			}


			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << pos; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_Webtalk_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case StdDispatchMethod::AUTHENTIFY_SHARED: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_Webtalk_ticks = SINGLETON(HPTIMER).ftime();
			

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << "80000006" << ") accept_connect_shared ( "; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_Webtalk_obj->accept_connect_shared(  );
			_coid_bstr << _coid_err_code;
			

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_Webtalk_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case StdDispatchMethod::AUTHENTIFY_AUTO: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::HANDLE_NET_LOOP: {
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::PREDESTRUCTOR: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_Webtalk_ticks = SINGLETON(HPTIMER).ftime();
			

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << "80000002" << ") accept_destroy ( "; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_Webtalk_obj->accept_destroy(  );
			if(coid_meta)  coid_meta->xstream_out(_coid_err_code,"err");  else  _coid_bstr << _coid_err_code;
			
			_coid_bstr.flush();

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_Webtalk_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case StdDispatchMethod::COMMAND: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::SPAWN: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::ECHO: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;


		/// service-specific methods:
		case COID_Webtalk_read_0: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_Webtalk_ticks = SINGLETON(HPTIMER).ftime();
			
			uint ref;
			if(coid_meta)  coid_meta->xstream_in(ref, "ref"); else  _coid_bstr >> ref;
			dynarray<token> rows;
			uint flg;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << COID_Webtalk_read_0 << ") read ( "; *_coid_log << ref; *_coid_log << ", "; *_coid_log << "?"; *_coid_log << ", "; *_coid_log << "?"; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_Webtalk_obj->read( ref, rows, &flg );
			if(coid_meta)  coid_meta->xstream_out(_coid_err_code,"err");  else  _coid_bstr << _coid_err_code;
			if( ! _coid_err_code ) {
				if(coid_meta)  coid_meta->xstream_out( ref, "ref" );  else  _coid_bstr << ref;
				if(coid_meta)  coid_meta->xstream_out( rows, "rows" );  else  _coid_bstr << rows;
				if(coid_meta)  coid_meta->xstream_out( flg, "flg" );  else  _coid_bstr << flg;
			}

			_coid_bstr.flush();

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << ref; *_coid_log << ", "; *_coid_log << rows; *_coid_log << ", "; *_coid_log << flg; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_Webtalk_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case COID_Webtalk_send_1: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_Webtalk_ticks = SINGLETON(HPTIMER).ftime();
			
			charstr text;
			if(coid_meta)  coid_meta->xstream_in(text, "text"); else  _coid_bstr >> text;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << COID_Webtalk_send_1 << ") send ( "; *_coid_log << text; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_Webtalk_obj->send( text );
			if(coid_meta)  coid_meta->xstream_out(_coid_err_code,"err");  else  _coid_bstr << _coid_err_code;
			
			_coid_bstr.flush();

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_Webtalk_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case COID_Webtalk_userlist_2: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_Webtalk_ticks = SINGLETON(HPTIMER).ftime();
			
			charstr usrs;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << COID_Webtalk_userlist_2 << ") userlist ( "; *_coid_log << "?"; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_Webtalk_obj->userlist( usrs );
			if(coid_meta)  coid_meta->xstream_out(_coid_err_code,"err");  else  _coid_bstr << _coid_err_code;
			if( ! _coid_err_code ) {
				if(coid_meta)  coid_meta->xstream_out( usrs, "usrs" );  else  _coid_bstr << usrs;
			}

			_coid_bstr.flush();

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << usrs; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_Webtalk_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case COID_Webtalk_smiles_3: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_Webtalk_ticks = SINGLETON(HPTIMER).ftime();
			
			const dynarray<Smiley> * smi;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << COID_Webtalk_smiles_3 << ") smiles ( "; *_coid_log << "?"; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_Webtalk_obj->smiles( smi );
			if(coid_meta)  coid_meta->xstream_out(_coid_err_code,"err");  else  _coid_bstr << _coid_err_code;
			if( ! _coid_err_code ) {
				if(coid_meta)  coid_meta->xstream_out( *smi, "smi" );  else  _coid_bstr << *smi;
			}

			_coid_bstr.flush();

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << *smi; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_Webtalk_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;


		default:
			_coid_bstr.acknowledge( true );
			_coid_bstr << ersNOT_IMPLEMENTED;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		break;
	}



	return _coid_err_code;
}












/// method table
static METHOD_TABLE COID_Webtalk_method_table[] = {
	{NULL, UMAX32},
	{NULL, UMAX32},
	{"read", COID_Webtalk_read_0},
	{"userlist", COID_Webtalk_userlist_2},
	{"send", COID_Webtalk_send_1},
	{"smiles", COID_Webtalk_smiles_3}
};



#define COID_dispatch_method_size		(sizeof(COID_Webtalk_method_table) / sizeof(METHOD_TABLE))



uint COID_Webtalk_8_0_get_method_id( const token & name )
{
	hash<token> mha;
	uints idx = mha(name) % COID_dispatch_method_size;

	for( uints i=0; i<COID_dispatch_method_size; i++ ) {
		if( COID_Webtalk_method_table[idx].name )
			if( name == COID_Webtalk_method_table[idx].name )
				return COID_Webtalk_method_table[idx].id;
		idx++;
		if( idx == COID_dispatch_method_size ) idx = 0;
	}

	return UMAX32;
}


} // namespace coid
